package com.taobao.freetest.ui.editer;

import org.eclipse.jface.preference.StringButtonFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

public abstract class AbstractStringButtonFieldEditor extends StringButtonFieldEditor implements Editor{


	public AbstractStringButtonFieldEditor(Composite parent){
		super();
		init(getName(), getLabelText());
		setChangeButtonText(getButtonText());
		setValidateStrategy(StringFieldEditor.VALIDATE_ON_FOCUS_LOST);
		setEmptyStringAllowed(isEmptyStringAllowed());
		setErrorMessage(getErrorMessage());
		createControl(parent);
		Button button = getChangeControl(parent);
		button.addFocusListener(getFocusListerner());
	}
	
	protected abstract String getName();

	protected String getButtonText(){
		return "check";
	}
	
	protected FocusListener getFocusListerner() {
		return new FocusListener() {
			public void focusGained(FocusEvent e) {
			}

			public void focusLost(FocusEvent e) {
				
				if (isValid()) {
					getPage().setMessage(null);
				}
			}
		};
	}
	
	@Override
	protected void doLoad() {
		super.doLoad();
		if(getStringValue()==null || "".equals(getStringValue())){
			setStringValue(getDefaultValue());
		}
	}

	protected String getDefaultValue() {
		return "";
	}
	
}
